/*Copyright (C) 2013-2021 LZE*/
#include<bits/stdc++.h>
#define fo(x) freopen(#x".in", "r", stdin); freopen(#x".out", "w", stdout);
#define INF 0x7fffffff
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1000010;
const int M = 100010;
ll n, m;
struct GraphEdge {
    ll to, next, w;
    GraphEdge() {}
} edge[M];
ll head[N] = {0};
ll num = 0;
void add(ll u, ll v, ll w) {
    edge[++num].to = v;
    edge[num].w = w;
    edge[num].next = head[u];
    head[u] = num;
}
ll cnt[N] = {0};
ll dis[N] = {0};
bool vis[N] = {0};
queue<ll> q;
bool spfa(const ll &x) {
    dis[x] = 0; q.push(x); vis[x] = true;
    ll p, to, w;
    while(!q.empty()) {
        p = q.front(); q.pop();
        vis[p] = false;
        for(ll i = head[p]; i; i = edge[i].next) {
            to = edge[i].to; w = edge[i].w;
            if(dis[to] > dis[p] + w) {
                dis[to] = dis[p] + w;
                if(!vis[to]) {
                    vis[to] = true;
                    q.push(to);
                    if(++cnt[to] >= n + 1) {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
int main() {
    scanf("%lld%lld", &n, &m);
    ll u, v, w;
    for(ll i = 1; i <= m; i++) {
        scanf("%lld%lld%lld", &u, &v, &w);
        add(v, u, w);
    }
    for(ll i = 1; i <= n; i++) add(N - 1, i, 0);
    for(ll i = 1; i <= n; i++) dis[i] = INF;
    if(!spfa(N - 1)) {
        printf("NO\n");
        return 0;
    }
    for(ll i = 1; i <= n; i++) printf("%lld ", dis[i]);
    printf("\n");
    return 0;
}
